package com.stlm2.dems.service;

import com.stlm2.dems.entity.DdmGoods;

import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.Specification;
import com.stlm2.dems.dao.SpecificationDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("specificationService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class SpecificationService extends BaseServiceImpl<Specification> {

	@Resource
	private SpecificationDao specificationDao;
	@Resource
	private DdmGoodsService ddmGoodsService;

	@Override
	protected BaseDao <Specification> getDao() {
		return specificationDao;
	}

    public List<Specification> getByGoodId(Integer goodId) {

		return specificationDao.getByGoodId(goodId);
	}

	public List<ModelMap> convent(List<Specification> specification) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(Specification s:specification){
			list.add(builderModel(s));
		}
		return list;
	}

	public ModelMap builderModel(Specification specification) {
		ModelMap map=new ModelMap();
		map.put("id",specification.getId());
		map.put("name",specification.getName());
		return map;
	}

	public List<ModelMap> conventAll(List<Specification> specification) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(Specification s:specification){
			list.add(builderModelAll(s));
		}
		return list;
	}
	public ModelMap builderModelAll(Specification specification) {
		ModelMap map=new ModelMap();
		DdmGoods goods = ddmGoodsService.findEntityById(specification.getGoodId());
		map.put("id",specification.getId());
		map.put("name",specification.getName());
		map.put("goodName",goods.getName());
		map.put("createTime", Convert.toFullYMDHMS(specification.getCreateTime()));
		map.put("isValid",specification.getIsValid()==true?"有效":"无效");
		return map;
	}
}